const minimatch = require('minimatch');
const evaluate = require('./e2e-eval');

const file_filters = {
    '.eslintrc.js': 'lint',
    '.eslintignore': 'lint',
    'config/test.env.js': 'unit || e2e',
    'build/webpack.test.conf.js': "unit && runner === 'karma'",
    'test/unit/**/*': 'unit',
    'test/unit/index.js': "unit && runner === 'karma'",
    'test/unit/jest.conf.js': "unit && runner === 'jest'",
    'test/unit/karma.conf.js': "unit && runner === 'karma'",
    'test/unit/specs/index.js': "unit && runner === 'karma'",
    'test/unit/setup.js': "unit && runner === 'jest'",
    'test/e2e/**/*': 'e2e',
    'src/router/**/*': 'router',
};

const data_capture = {
    destDirName: "mark",
    inPlace: false,
    noEscape: true,
    isNotTest: true,
    name: "mark",
    description: "A Vue.js project",
    author: 'LongXiaoBai <wlan945636373@gmail.com>',
    build: "standalone",
    router: true,
    lint: true,
    lintConfig: 'standard',
    unit: true,
    runner: 'jest',
    e2e: true,
    autoInstall: "npm"
};

/**
 * 过滤文件
 * minimatch: 模式匹配字符串
 * @param {*} files 
 * @param {*} data 
 * @param {*} done 
 */
const filter = (files, filters, data, done) => {
    let results = [],
        file_names = Object.keys(files),
        data_filters = Object.entries(filters || {});

    for (const [name_pattern, express] of data_filters) {
        results = minimatch.match(file_names, name_pattern);
        for (const file_name of results) {
            if (evaluate(express, data)) {
                delete files[file_name];
            }
        }
    }

    done();
}


const defineFn = (files, filters, data, done) => {
    if (!filters) {
        return done();
    }

    const fileNames = Object.keys(files);
    Object.keys(filters).forEach(glob => {
        fileNames.forEach(file => {
            if (minimatch(file, glob, { dot: true })) {
                const condition = filters[glob];

                if (!evaluate(condition, data)) {
                    delete files[file];
                }
            }
        })
    })
    done();
}

const files = {
    '.babelrc': 'babelrc',
    '.editorconfig': 'babelrc',
    '.eslintignore': 'babelrc',
    '.gitignore': 'babelrc',
    '.eslintrc.js': 'babelrc',
    '.postcssrc.js': 'babelrc',
    'package.json': 'babelrc',
    'index.html': 'babelrc',
    'README.md': 'babelrc',
    'config\\dev.env.js': 'babelrc',
    'config\\prod.env.js': 'babelrc',
    'config\\index.js': 'babelrc',
    'config\\test.env.js': 'babelrc',
    'build\\build.js': 'babelrc',
    'build\\check-versions.js': 'babelrc',
    'build\\utils.js': 'babelrc',
    'build\\logo.png': 'babelrc',
    'build\\vue-loader.conf.js': 'babelrc',
    'build\\webpack.base.conf.js': 'babelrc',
    'build\\webpack.dev.conf.js': 'babelrc',
    'build\\webpack.prod.conf.js': 'babelrc',
    'build\\webpack.test.conf.js': 'babelrc',
    'static\\.gitkeep': 'babelrc',
    'src\\App.vue': 'babelrc',
    'src\\main.js': 'babelrc',
    'src\\assets\\logo.png': 'babelrc',
    'src\\components\\HelloWorld.vue': 'babelrc',
    'src\\router\\index.js': 'babelrc',
    'test\\e2e\\nightwatch.conf.js': 'babelrc',
    'test\\e2e\\runner.js': 'babelrc',
    'test\\e2e\\custom-assertions\\elementCount.js': 'babelrc',
    'test\\e2e\\specs\\test.js': 'babelrc',
    'test\\unit\\.eslintrc': 'babelrc',
    'test\\unit\\index.js': 'babelrc',
    'test\\unit\\jest.conf.js': 'babelrc',
    'test\\unit\\karma.conf.js': 'babelrc',
    'test\\unit\\setup.js': 'babelrc',
    'test\\unit\\specs\\HelloWorld.spec.js': 'babelrc'
}

const e2eFn = (fn, markdown) => {
    console.time(`log ${markdown}`);
    fn && fn(files, file_filters, data_capture, () => { console.log(`done ${markdown}`) });
    console.timeEnd(`log ${markdown}`);
}

// filter(files, file_filters, data_capture, () => { console.log('done console') });

e2eFn(defineFn, 'defineFn'); // log defineFn: 13.854ms
e2eFn(filter, 'filter'); // log filter: 2.428ms

module.exports = filter;